//DSA ASSIGNMENT-1
//ANKUR GOEL
//BTECH 2010
//ROLL NUMBER - 2010016
//Implementation of list attribute of python in c language using arrays


#include<stdio.h>
#include<stdlib.h>


int *list , *list2;
int size=1 , size2=1;



void showlist(int *a ,int n)
	{
		printf("\n\n The list is : ");
		int count=0;
		printf(" [ ");
		while(count<n-1)
			{	
				printf("%d , " , *(a+count));
				count++;
			}
		printf("%d ]\n\n" , *(a+count));
	}


int * createlist(int *a ,int n )
	{	a=malloc(1*sizeof(int));
		int count=0;
		int yesno;
		n=1;
		

		do
		 {      
			printf("\n\n Enter element number %d of your new list : " , count+1);
			scanf("%d" , (a+count));
			count++;
			showlist(a , n);
			printf("\n Do you want to enter more elements (1--> yes , 2--> no ) : ");
			scanf("%d" , &yesno);	
			
			if(yesno==1)
				{	n++;
					a=realloc(a,n*sizeof(int)); 	
				}			
						

			while((yesno!=1)&&(yesno!=2))
				{
					printf("\n\n You entered an incorrect choice , please enter again !! \n");
					printf("\n Do you want to enter more elements (1--> yes , 2--> no ): ");
					scanf("%d" , &yesno);		
					if(yesno==1)
						{	n++;
							a=realloc(a,n*sizeof(int)); 	
						}		
				}		

		 }while(yesno == 1);
		 showlist(a , n);
		 size=n;
		 return a;
		

	}		


int * createlist2(int *a ,int n )
	{	a=malloc(1*sizeof(int));
		int count=0;
		int yesno;
		n=1;
		

		do
		 {      
			printf("\n\n Enter element number %d of your new list : " , count+1);
			scanf("%d" , (a+count));
			count++;
			showlist(a , n);
			printf("\n Do you want to enter more elements (1--> yes , 2--> no ) : ");
			scanf("%d" , &yesno);	
			
			if(yesno==1)
				{	n++;
					a=realloc(a,n*sizeof(int)); 	
				}			
						

			while((yesno!=1)&&(yesno!=2))
				{
					printf("\n\n You entered an incorrect choice , please enter again !! \n");
					printf("\n Do you want to enter more elements (1--> yes , 2--> no ): ");
					scanf("%d" , &yesno);		
					if(yesno==1)
						{	n++;
							a=realloc(a,n*sizeof(int)); 	
						}		
				}		

		 }while(yesno == 1);
		 showlist(a , n);
		 size2=n;
		 return a;
		

	}	


int * append(int *a ,int n)
	{	
		showlist(a , n);
		n++;
		a=realloc(a,n*sizeof(int)); 
		

		int count=0,x,yesno=2;
		while(yesno==2)
			{		
				printf("\n\n Enter the element that you wish to append in the list : ");
				scanf("%d" , &x);
				printf("\n The element you entered is : %d" , x);
				printf("\n Is this the element that you wish to append ( 1--> yes , 2--> no , i`ll enter element again ) :");
				scanf("%d" , &yesno);	
				while((yesno!=1)&&(yesno!=2))
					{
						printf("\n\n You entered an incorrect choice , please enter again !! \n");
						printf("\n The element you entered is : %d" , x);
						printf("\n Is this the list that you wish to append ( 1--> yes , 2--> no , i`ll enter element again ) : ");
						scanf("%d" , &yesno);	
					}

			}
		
		*(a+n-1)=x;
		printf("\n\n SUCCESS !!! Element has been added at end of list \n");
		showlist(a , n);
		size=n;
		return a;
	}	

	


int * extend(int *a ,int n)
	{
		int yesno=2;		
		
		while(yesno==2)
			{
				list2=createlist2(list2 , size2);	
				printf("\n Is this the list that you wish to extend into your list ( 1--> yes , 2--> no , i`ll enter list again ) :");
				scanf("%d" , &yesno);	
				while((yesno!=1)&&(yesno!=2))
					{
						printf("\n\n You entered an incorrect choice , please enter again !! ");
						showlist(list2 , size2);
						printf("\n Is this the list that you wish to extend into your list ( 1--> yes , 2--> no , i`ll enter list again ) : ");
						scanf("%d" , &yesno);	
					}

			}
		
		
		int count=0,m;
		m=n+size2;
		a=realloc(a,(n+size2)*sizeof(int)); 
		while(n<m)
			{
				*(a+n)=*(list2+count);
				n++;
				count++;
			}


		printf("\n\n SUCCESS !!! Your list has been extended. \n");
		showlist(a,n);
		size=n;
		return a;
	}			



int * insert(int *a,int n)
	{
		int pos , temp=0 ,store, x , yesno=2;
		showlist(a , n );
		while(yesno==2)
			{		
				printf("\n\n Enter the element that you wish to append in the list : ");
				scanf("%d" , &x);
				printf("\n The element you entered is : %d" , x);
				printf("\n Is this the element that you wish to append ( 1--> yes , 2--> no , i`ll enter element again ) :");
				scanf("%d" , &yesno);	
				while((yesno!=1)&&(yesno!=2))
					{
						printf("\n\n You entered an incorrect choice , please enter again !! \n");
						printf("\n The element you entered is : %d" , x);
						printf("\n Is this the element that you wish to append ( 1--> yes , 2--> no , i`ll enter element again ) : ");
						scanf("%d" , &yesno);	
					}

			}

		yesno=2;
		while(yesno==2)
			{		
				printf("\n\n Enter the position at which you wish to append in the list : ");
				scanf("%d" , &pos);
				printf("\n The position you entered is : %d" , pos);
				printf("\n Is this the position that you wish to append at ( 1--> yes , 2--> no , i`ll enter position again ) :");
				scanf("%d" , &yesno);	
				while((yesno!=1)&&(yesno!=2))
					{
						printf("\n\n You entered an incorrect choice , please enter again !! \n");
						printf("\n The position you entered is : %d" , pos);
						printf("\n Is this the position that you wish to append at ( 1--> yes , 2--> no , i`ll enter position again ) : ");
						scanf("%d" , &yesno);	
					}	
 
			}

		
		if((pos-1)>=n)
			{
				printf("\n\n FAILED !!! Your element %d hasnt been inserted in the list at position entered as the position doesnt exist in the list.\n" , x );
				showlist(a,n);
			}
		else
			{
				n++;
				a=realloc(a,n*sizeof(int)); 


				temp=n-1;
				while(temp>pos-1)
				   {
					store=*(a+temp-1);
					*(a+temp-1)=*(a+temp);
					*(a+temp)=store;
					temp--;
				   }
	
				*(a+pos-1)=x;
				printf("\n\n SUCCESS !!! Your element %d has been inserted in the list at position %d. \n" , x , pos);
				showlist(a , n);
			}
		size=n;
		return a;
	}


int * removal(int *a ,int n)
	{
		int temp=0 ,flag=0,store, x , yesno;
		
		showlist(a , n);
		do
			{		
				printf("\n\n Enter the element that you wish to remove from the list : ");
				scanf("%d" , &x);
				printf("\n The element you entered is : %d" , x);
				printf("\n Is this the element that you wish to remove ( 1--> yes , 2--> no , i`ll enter element again ) : ");
				scanf(" %d" , &yesno);	
				while((yesno != 1)&&(yesno != 2))
					{
						printf("\n\n You entered an incorrect choice , please enter again !! \n");
						printf("\n The element you entered is : %d" , x);
						printf("\n Is this the element that you wish to remove ( 1--> yes , 2--> no , i`ll enter element again ) : ");
						scanf(" %d" , &yesno);	
					}
			}while(yesno==2);

		while(temp<n)
			{		
				if(*(a+temp)==x)
		 			{	
						flag=1;
						while((temp+1)<n)
		   				{
							store=*(a+temp+1);
							*(a+temp+1)=*(a+temp);
							*(a+temp)=store;
							temp++;
			        		}
						n--;
						a=realloc(a,n*sizeof(int)); 
						printf("\n\n SUCCESS !!! Your element %d has been removed from the list at its first occurence . \n" , x );
						showlist(a,n);
						break;
					}
				temp++;
			}

		if(flag==0)
			{
				printf("\n\n FAILED !!! Your element %d has not been found in the list . \n" , x );
				showlist(a,n);
			}
		size=n;
		return a;
	
	
	}
			
		

int pop(int *a,int n)
	{
		int yesno=2 , pos , store , popreturn=-1;
		showlist(a,n);
		while(yesno==2)
			{		
				printf("\n\n Enter the position at which you wish to pop the element in the list : ");
				scanf("%d" , &pos);
				printf("\n The position you entered is : %d" , pos);
				printf("\n Is this the position that you wish to pop at ( 1--> yes , 2--> no , i`ll enter position again ) :");
				scanf("%d" , &yesno);	
				while((yesno!=1)&&(yesno!=2))
					{	
						printf("\n\n You entered an incorrect choice , please enter again !! \n");
						printf("\n The position you entered is : %d" , pos);
						printf("\n Is this the position that you wish to pop at ( 1--> yes , 2--> no , i`ll enter position again ) : ");
						scanf("%d" , &yesno);	
					}

			}
		pos--;

		if(pos>=n)
			{
				printf("\n\n FAILED !!! The list isnt large enough , there is no element at the entered position.\n ");
				showlist(a,n);
			}

		else
			{
	
				while((pos+1)<n)
	   				{
						store=*(a+pos+1);
						*(a+pos+1)=*(a+pos);
						*(a+pos)=store;
						pos++;
        				}
				popreturn=*(a+pos);
				n--;			
				a=realloc(a,n*sizeof(int)); 
				printf("\n\n SUCCESS !!! The element popped at the entered position is : %d \n" , popreturn );
				showlist(a,n);
			}
		size=n;
		return popreturn;
	}



int indexs(int *a,int n)
	{
		int yesno , indexreturn=-1 , temp=0 , x;
		showlist(a,n);
		
		do
			{		
				printf("\n\n Enter the element that you wish to find the first occurence of : ");
				scanf("%d" , &x);
				printf("\n The element you entered is : %d" , x);
				printf("\n Is this the element that you wish to find the first occurence of : ( 1--> yes , 2--> no , i`ll enter element again ) : ");
				scanf(" %d" , &yesno);	
				while((yesno != 1)&&(yesno != 2))
					{
						printf("\n\n You entered an incorrect choice , please enter again !! \n");
						printf("\n The element you entered is : %d" , x);
						printf("\n Is this the element that you wish to find the first occurence of : ( 1--> yes , 2--> no , i`ll enter element again ) : ");
						scanf(" %d" , &yesno);	
					}

			}while(yesno==2);

		while(temp<n)
			{		
				if(*(a+temp)==x)
		 			{	
						indexreturn=temp+1;
						break;
					}
				temp++;
			}

		if(indexreturn==-1)
			printf("\n\n FAILED !!! The entered element was not found in the list.\n ");

		else
			printf("\n\n SUCCESS !!! The first occurence of the entered element %d was found at the position : %d \n" , x , indexreturn);
		showlist(a,n);	
		size=n;
		return indexreturn;

	}
								

int count(int *a,int n)
	{
		int yesno , countreturn=0 , temp=0 , x;
		showlist(a,n);
		
		do
			{		
				printf("\n\n Enter the element that you wish to find the times of occurence of : ");
				scanf("%d" , &x);
				printf("\n The element you entered is : %d" , x);
				printf("\n Is this the element that you wish to find the times of occurence of : ( 1--> yes , 2--> no , i`ll enter element again ) : ");
				scanf(" %d" , &yesno);	
				while((yesno != 1)&&(yesno != 2))
					{
						printf("\n\n You entered an incorrect choice , please enter again !! \n");
						printf("\n The element you entered is : %d" , x);
						printf("\n Is this the element that you wish to find the times of occurence of : ( 1--> yes , 2--> no , i`ll enter element again ) : ");
						scanf(" %d" , &yesno);	
					}

			}while(yesno==2);

		
		while(temp<n)
			{		
				if(*(a+temp)==x)
		 			{	
						countreturn++;
					}
				temp++;
			}
	
		if(countreturn==0)
			{
				printf("\n\n FAILED !!! The entered element was not found in the list.\n ");
				countreturn=-1;
			}
				
		else
			printf("\n\n SUCCESS !!! The times of occurence of the entered element %d in the list is : %d \n" , x , countreturn);

		showlist(a,n);	
		return countreturn;

	}


int * sort(int *a,int n)
	{
		int temp=0 , small , pos , store ;
		showlist(a,n);
		while(temp<n)
			{
				small=*(a+temp);
				int i=temp+1;
				while(i<n)
					{
						if(*(a+i)<small)
						   {	
							small=*(a+i);
							pos=i;
						   }	
						i++;		
					}
				if(small!=*(a+temp))
					{
						store=*(a+temp);
						*(a+temp)=small;
						*(a+pos)=store;
					}
				temp++;
			}
		printf("\n\n SUCCESS !!! The list has been sorted. \n");
		showlist(a,n);				
		return a;	
	}


int * reverse(int *a,int n)
	{
		int count=0,temp=n-1,store;
		showlist(a,n);
		while(temp>=count)
			{	store=*(a+temp);
				*(a+temp)=*(a+count);
				*(a+count)=store;
				count++;
				temp--;
			}
		
		printf("\n\n SUCCESS !!! The list has been reversed. \n");
		showlist(a,n);	
		return a;			
	}



int listsize(int *a,int n)
	{
		int elementcount=n;
		showlist(a,n);

		printf("\n\n The number of elements in the list is : %d \n\n" , elementcount);

		if(elementcount==0)
			elementcount=-1;
		return elementcount;
	}
		

void main()
{
	
	int popreturn=-1;
	int indexreturn=-1 , countreturn=-1 , elementcount=-1;
	int choice=0,flag=0;
	while (choice!=13)
		{
			if(flag==0)
				{
				  flag=1;
			  	  list=createlist(list,size);
				 
				
				}
	
			printf("\n    	         MAIN MENU !!!     \n");
			printf("\n	 What do you want to do ??? \n");
			printf("\n	 1.  CREATE  --- Discard your present list and make a new list.");
			printf("\n	 2.  APPEND  --- Append  an item at the end of your list.");
			printf("\n	 3.  EXTEND  --- Extend the list by appending all the items in a list at the end.");
			printf("\n	 4.  INSERT  --- Add an element in the list at a particular position.");
			printf("\n	 5.  REMOVE  --- Remove the first occurence of an element from your list.");
			printf("\n 	 6.  POP     --- Pop the item at a given position in your list and return it.");
			printf("\n	 7.  INDEX   --- Return the index of the first occurence of an item in your list.");
			printf("\n	 8.  COUNT   --- Return the number of times an element appears in the list");
			printf("\n	 9.  SORT    --- Sort the elements of the list.");
			printf("\n	 10. REVERSE --- Reverse the elements of your list.");
			printf("\n	 11. SHOW    --- Displays the list.");
			printf("\n	 12. SIZE    --- Returns the total number of elements in the list.");
			printf("\n	 13. EXIT    --- Exit the program.");
			printf("\n\n     Enter the function number of your choice : ");
			scanf("%d" , &choice);

			int checkswitch=0;
			while(checkswitch==0)
			{
			   switch(choice)
				{
					case 1 : list=createlist(list,size);
						 checkswitch=1;
						 break;
					
					case 2 : list=append(list,size);
						 checkswitch=1;
						 break;

					case 3 : list=extend(list,size);
						 checkswitch=1;
						 break;

					case 4 : list=insert(list,size);
						 checkswitch=1;
						 break;

					case 5 : list=removal(list,size);
						 checkswitch=1;
						 break;

					case 6 : popreturn=pop(list,size);
						 checkswitch=1;
						 break;

					case 7 : indexreturn=indexs(list,size);
						 checkswitch=1;
						 break;

					case 8 : countreturn=count(list,size);
						 checkswitch=1;
						 break;

					case 9 : list=sort(list,size);
						 checkswitch=1;
						 break;

					case 10 : list=reverse(list,size);
						 checkswitch=1;
						 break;

					case 11 : showlist(list,size);
						  checkswitch=1;
						  break;
					case 12 : elementcount=listsize(list,size);
						  checkswitch=1;
						  break;


					case 13 : printf("\n\n Thank you for using this program.   \n\n ");
						  checkswitch=1;
						  break;

					default :
						  printf("\n\n The choice you entered wasnt valid !! \n Please choose again !!\n");
						  choice=0;
					
						  while((choice<1)||(choice>10))
							{
					 			printf("\n\n     Enter the function number of your choice : ");
								scanf("%d" , &choice);
							}
				
				}
		
			}

		}

}
